// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HsbcDataParser.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="HsbcDataParser" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Business.Parser
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Entities;
    using LINQtoCSV;

    /// <summary>
    /// Defines the <seealso cref="HsbcDataParser"/> type.
    /// </summary>
    public class HsbcDataParser : IBankDataParser
    {
        /// <summary>
        /// Imports the CSV.
        /// </summary>
        /// <param name="fileName">The data.</param>
        /// <returns>Transactions imported from csv</returns>
        public IEnumerable<Transaction> ImportCsv(string fileName)
        {
            var context = new CsvContext();
            var fixedFileName = PreProcessFile(fileName);
            var hsbcData = context.Read<HsbcDataItem>(fixedFileName, new CsvFileDescription { SeparatorChar = ',' });

            var result = hsbcData.Select(dataItem => new Transaction
                {
                    Date = dataItem.Date,
                    Title = dataItem.Title,
                    Amount = Math.Abs(dataItem.Value),
                    TransactionType = dataItem.Value < 0 ? TransactionTypes.Expanse : TransactionTypes.Income
                }).ToList();

            return result.ToList();
        }

        /// <summary>
        /// Pres the process file.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <returns>Path of file that is fixed. This is a file created in temporary folder and will be cleaned up automatically</returns>
        private string PreProcessFile(string fileName)
        {
            var fixedFile = Path.GetTempFileName();
            using (var wtr = new StreamWriter(fixedFile))
            {
                wtr.WriteLine("Date,Description,Amount");
                foreach (var line in File.ReadAllLines(fileName))
                {
                    var trimmedLine = line.Trim(new[] { ',' });
                    var splitParts = trimmedLine.Split(new[] { ',' });
                    if (splitParts.Length > 3)
                    {
                        wtr.Write(splitParts[0] + ",");
                        wtr.Write(splitParts[1]);
                        wtr.Write(splitParts[2] + ", ");
                        wtr.WriteLine(splitParts[3]);
                    }
                    else
                    {
                        wtr.WriteLine(trimmedLine);
                    }
                }

                wtr.Flush();
            }

            return fixedFile;
        }

        /// <summary>
        /// Defines the <seealso cref="HsbcDataItem"/> type.
        /// </summary>
        public class HsbcDataItem
        {
            /// <summary>
            /// Gets or sets the date.
            /// </summary>
            /// <value>
            /// The date.
            /// </value>
            [CsvColumn(Name = "Date", FieldIndex = 0)]
            public DateTime Date { get; set; }

            /// <summary>
            /// Gets or sets the description.
            /// </summary>
            /// <value>
            /// The description.
            /// </value>
            [CsvColumn(Name = "Description", FieldIndex = 1)]
            public string Title { get; set; }

            /// <summary>
            /// Gets or sets the value.
            /// </summary>
            /// <value>
            /// The value.
            /// </value>
            [CsvColumn(Name = "Amount", FieldIndex = 2)]
            public decimal Value { get; set; }
        }
    }
}